# -*- coding:utf-8 -*-
from PySide6.QtCore import QObject, Signal,QDir
from app.common.global_config import GlobalConfig

class AppModel(QObject):
    """
    mvc model 实现
    """
    # 新增信号
    # protoc文件目录变化信号 
    protoc_dir_changed = Signal(str)
    # proto文件目录变化信号 
    proto_dir_changed = Signal(str)
    # server文件目录变化信号
    server_dir_changed = Signal(str)
    # client文件目录变化信号
    client_dir_changed = Signal(str)
    # server导出类型变化信号
    server_export_type_changed = Signal(str)
    # client导出类型变化信号
    client_export_type_changed = Signal(str)
    # 日志变化信号
    logging_changed = Signal(str) 

    def __init__(self):
        super().__init__()
        self._protoc_dir = ""
        self._proto_dir = ""
        self._server_export_type = ""
        self._server_dir = ""
        self._client_export_type = ""
        self._client_dir = ""
        self._log_content = ""

    def protoc_dir(self):
        return self._protoc_dir

    def set_protoc_dir(self, value):
        if self._protoc_dir != value:
            self._protoc_dir = value
            self.protoc_dir_changed.emit(value)
            # 更新配置项
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.PROTOC_DIR_KEY, self._protoc_dir)

    def proto_dir(self):
        return self._proto_dir

    def set_proto_dir(self, value):
        if self._proto_dir != value:
            self._proto_dir = value
            self.proto_dir_changed.emit(value)
            # 更新配置项
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.PROTO_DIR_KEY, self._proto_dir)

    def server_export_type(self):
        return self._server_export_type
    
    def set_server_export_type(self, value):
        self._server_export_type = value
        self.server_export_type_changed.emit(value)
        # 更新配置
        global_config = GlobalConfig.get_instance()
        global_config.set(global_config.SERVER_EXPORT_TYPE_KEY, self._server_export_type)


    def server_dir(self):
        return self._server_dir

    def set_server_dir(self, value):
        if self._server_dir != value:
            self._server_dir = value
            self.server_dir_changed.emit(value)
            # 更新配置项
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.SERVER_DIR_KEY, self._server_dir)

    def client_export_type(self):
        return self._client_export_type
    
    def set_client_export_type(self, value):
        self._client_export_type = value
        self.client_export_type_changed.emit(value)
        # 更新配置项
        global_config = GlobalConfig.get_instance()
        global_config.set(global_config.CLIENT_EXPORT_TYPE_KEY, self._client_export_type)

    def client_dir(self):
        return self._client_dir

    def set_client_dir(self, value):
        if self._client_dir != value:
            self._client_dir = value
            self.client_dir_changed.emit(value)
            # 更新配置项
            global_config = GlobalConfig.get_instance()
            global_config.set(global_config.CLIENT_DIR_KEY, self._client_dir)

    def log_content(self):
        return self._log_content

    def set_log_content(self, value):
        self._log_content = f"[{QDir.currentPath()}] {value}\n{self._log_content}"
        self.logging_changed.emit(self._log_content)

    def init_model(self, data):
        """
        去读配置表信息,初始化model信息
        """
        global_config = GlobalConfig.get_instance()
        if global_config.PROTOC_DIR_KEY in data:
            protoc_dir = data[global_config.PROTOC_DIR_KEY]
            self.set_protoc_dir(protoc_dir)
        if global_config.PROTO_DIR_KEY in data:
            proto_dir = data[global_config.PROTO_DIR_KEY]
            self.set_proto_dir(proto_dir)
        if global_config.SERVER_EXPORT_TYPE_KEY in data:
            server_export_type = data[global_config.SERVER_EXPORT_TYPE_KEY]
            self.set_server_export_type(server_export_type)
        if global_config.SERVER_DIR_KEY in data:
            server_export_dir = data[global_config.SERVER_DIR_KEY]
            self.set_server_dir(server_export_dir)
        if global_config.CLIENT_EXPORT_TYPE_KEY in data:
            client_export_type = data[global_config.CLIENT_EXPORT_TYPE_KEY]
            self.set_client_export_type(client_export_type)
        if global_config.CLIENT_DIR_KEY in data:
            client_export_dir = data[global_config.CLIENT_DIR_KEY]
            self.set_client_dir(client_export_dir)  

    def save_config(self):
        """
        保存配置到文件
        """
        global_config = GlobalConfig.get_instance()
        global_config.set(global_config.PROTOC_DIR_KEY, self._protoc_dir)
        global_config.set(global_config.PROTO_DIR_KEY, self._proto_dir)
        global_config.set(global_config.SERVER_EXPORT_TYPE_KEY, self._server_export_type)
        global_config.set(global_config.SERVER_DIR_KEY, self._server_dir)
        global_config.set(global_config.CLIENT_EXPORT_TYPE_KEY, self._client_export_type)     
        global_config.set(global_config.CLIENT_DIR_KEY, self._client_dir)  